package com.example.company.vm

import androidx.lifecycle.MutableLiveData
import com.example.common.view.vm.BaseViewModel
import com.example.company.entity.CompanyDetailEntity
import com.example.company.entity.CompanyEntity
import com.example.company.entity.GetMerChant
import com.example.company.entity.MerChant
import com.example.company.entity.MerChantDetail
import com.example.company.entity.MerChantDetailEntity
import com.example.company.model.CompanyRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import okhttp3.RequestBody
import javax.inject.Inject
@HiltViewModel
class CompanyViewModel @Inject constructor(private val repo:CompanyRepository):BaseViewModel() {
    val mCompanySuccess = MutableLiveData<List<CompanyEntity>>()
    fun getCompany(){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getCompany()
            if(code == 200){
                mCompanySuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mCompanyDetailSuccess =MutableLiveData<CompanyDetailEntity>()
    fun getCompanyDetail(id:Int){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getCompanyDetail(id)
            if(code == 200){
                mCompanyDetailSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mMerChantSuccess = MutableLiveData<MerChant>()
    fun postMerChant(body:MerChant){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.postMerchant(body)
            if(code == 200){
               mMerChantSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mGetMerChantSuccess = MutableLiveData<List<GetMerChant>>()
    fun getMerChant(){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getMerChant()
            if(code == 200){
                mGetMerChantSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mMerChantDetailSuccess = MutableLiveData<MerChantDetail>()
    fun getMerChantDetail(id:Int){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getMerChantDetail(id)
            if(code == 200){
                mMerChantDetailSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
}